package com.example.lopngoaingu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tttrung43.libs.android.DBAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class VuonCoTichActivity extends Activity {

	private Button btn;
	private Handler handler;
	private Runnable task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vuoncotich_layout);
		btn = (Button) findViewById(R.id.btn);

		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new DoTask().execute();
			}
		});
	}

	void CreateSubject() {
		DBAdapter db = new DBAdapter(getApplicationContext());
		SQLiteDatabase db1 = db.getWritableDatabase();
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Truyện cổ Việt Nam" });
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Truyện cổ Anderson" });
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Truyện cổ thế giới" });
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Truyền thuyết & giai thoại" });
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Truyện cổ Grim" });
		db1.execSQL("insert into chude(tenchude) values(?)",
				new String[] { "Thần thoại Hy Lạp" });
		db1.close();
		db.close();
	}

	class DoTask extends AsyncTask<Void, Void, Void> {

		private ProgressDialog dialog;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(VuonCoTichActivity.this);
			dialog.setTitle("Loading");
			dialog.setMessage("Loading ......");
			dialog.show();			
		}
		@Override
		protected Void doInBackground(Void... params) {
			String[] urls = new String[] {
					"http://vanhoc.xitrum.net/truyencotich/vietnam/",
					"http://vanhoc.xitrum.net/truyencotich/vietnam/trang/2.html",
					"http://vanhoc.xitrum.net/truyencotich/andersen/",
					"http://vanhoc.xitrum.net/truyencotich/thegioi/",
					"http://vanhoc.xitrum.net/truyencotich/truyenthuyet/",
					"http://vanhoc.xitrum.net/truyencotich/grim/trang/1.html",
					"http://vanhoc.xitrum.net/truyencotich/thanthoai/" };
			String[] sub = new String[] { "1", "1", "2", "3", "4", "5", "6" };
			for (int i = 0; i < urls.length; i++)
				CreateContent(urls[i], sub[i]);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {		
			super.onPostExecute(result);
			dialog.dismiss();
		}
		
		void CreateContent(String address, String subject) {
			try {
				Document doc = Jsoup.connect(address).get();
				Elements elems = doc.select(".subleft");
				for (int i = 0; i < elems.size() - 6; i++) {
					String title = elems.get(i).text();
					String url = "http://vanhoc.xitrum.net"
							+ elems.get(i).attr("href");
					Document mDoc = Jsoup.connect(url).get();
					Element mContent = mDoc.select(".content").first();
					DBAdapter conn = new DBAdapter(VuonCoTichActivity.this);
					SQLiteDatabase db = conn.getWritableDatabase();
					db.execSQL("insert into truyen(tieude,noidung,yeuthich,chudeid) values(?,?,?,?)",new String[]{title,mContent.toString(),"0",subject});
					db.close();
					conn.close();
				}
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
			}
		}

	}
}
